<template>
	<view class="ts-flex ts-column">
		<!-- 弹出窗口 -->
		<ts-popup v-model="hidden">
			<view class="ts-grid-9">
				<view class="ts-grid-9-item" hover-class="ts-grid-9-item-hover" @tap="navigateTo('add')">
					<ts-icon type="plus" size="80" color="#d81e06"></ts-icon>
					<text class="ts-grid-9-text ts-padding">发布</text>
				</view>
				<view class="ts-grid-9-item" hover-class="ts-grid-9-item-hover" @tap="navigateTo('my')">
					<ts-icon type="person" size="80" color="#d81e06"></ts-icon>
					<text class="ts-grid-9-text ts-padding">我的</text>
				</view>
			</view>
			<button type="warn" style="width: 100%; background-color: #e31436; 	border-radius: 0upx; height: 100upx;" @tap="hide">关闭</button>
		</ts-popup>
		<!-- 弹出窗口 -->

		<!-- FAB -->
		<view class="ts-fab" @tap="showPopupWindow">
			<image src="../../static/plus.png" />
			<text v-show="messageCount > 0" class="ts-badge ts-badge-danger">{{messageCount}}</text>
		</view>
		<!-- FAB -->

		<ts-banner image='https://via.placeholder.com/750x250' height="250"></ts-banner>

		<ts-search-bar v-model="keywords" @search="doSearch"></ts-search-bar>

		<ts-segmented-control :values="tabs" v-model="currentTab" style-type="text" active-color="#d81e06" @clickItem="clickItem"
		 borderColor="#d81e06"></ts-segmented-control>


		<view class="ts-flex ts-column ts-flex-item">
			<view class="ts-list">
				<view class="ts-list-cell" v-for="(item,index) in list" hover-class="navigator-hover" :key="index" @tap="navigateToDetail(item.id)">
					<view class="ts-media-list">

						<image class="ts-media-list-logo" :src="item.cover_image"></image>
						<view class="ts-flex-item ts-column" style="justify-content: space-between;">
							<view class="ts-h5 ts-ellipsis-2">{{item.title}}</view>
							<view class="ts-h5">{{item.organizer}}</view>
							<view class="ts-h5">{{item.address}}</view>
							<view class="ts-h5">{{item.time}}</view>
							<view class="ts-row">
								<ts-badge :text="item.type" type="primary"></ts-badge>
								<ts-badge :text="item.status" type="danger" :padding-left="10"></ts-badge>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>


		<view class="ts-flex ts-column">
			<ts-load-more :loadingType="loadingType" :contentText="contentText" @tap="getEventListMoreData"></ts-load-more>
		</view>

	</view>
</template>

<script>
	import tsSegmentedControl from 'components/teaset/components/ts-segmented-control.vue';
	import tsBanner from 'components/teaset/components/ts-banner.vue';
	import tsSearchBar from 'components/teaset/components/ts-search-bar.vue';
	import tsPopup from 'components/teaset/components/ts-popup.vue';

	// var dateUtils = require('common/util.js').dateUtils;

	var self;
	export default {
		components: {
			tsBanner,
			tsSegmentedControl,
			tsSearchBar,
			tsPopup
		},
		data() {
			return {

				hidden: true,
				currentTab: 0,
				tabs: ['全部', '学校', '院系', '班级', '社团'],
				keywords: '',
				loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
				page: 0,
				list: []
			}
		},
		onLoad() {
			self = this;
			this.doSearch();
		},
		watch: {
			// 			currentTab(val) {
			// 
			// 				console.log('currentTab:' + val)
			// 				this.doSearch();
			// 			}
		},
		onPullDownRefresh() {
			self.doSearch()
		},
		onReachBottom() {
			console.log('onReachBottom');
			this.getEventListMoreData();
		},
		computed: {
			messageCount() {
				return this.list.length;
			}
		},
		methods: {
			showPopupWindow() {
				this.hidden = false;
			},
			hide() {
				this.hidden = true;
			},
			navigateTo(url) {
				this.hide();
				if (url) {
					uni.navigateTo({
						url: url,
					})
				}
			},
			navigateToDetail(id) {
				uni.navigateTo({
					url: `detail?id=${id}`
				});
			},
			callTelphone(tel) {
				uni.makePhoneCall({
					phoneNumber: tel
				});
			},
			clickItem(item) {

				console.log('onClickItem'+item);

				self.doSearch()
			},
			doSearch(keywords = '') {
				//重置状态数据
				this.list = [];
				this.page = 0;
				this.loadingType = 0;
				this.getEventListMoreData();
			},
			getEventListMoreData() {
				//上拉的状态：0-loading前；1-loading中；2-没有更多了
				if (this.loadingType !== 0) {
					return;
				}
				this.loadingType = 1;
				this.page = this.page + 1;

				uni.request({
					url: this.$API_BASE + `/app/activity/getEventList`,
					method: 'GET',
					dataType: 'json',
					data: {
						keywords: self.keywords,
						page: self.page,
						type: self.tabs[self.currentTab],
					},
					success(response) {
						const res = response.data;

						// console.log(JSON.stringify(res));
						//           {
						//             data:[],
						//             success:true,
						//             has_more:true,
						//             page:1,
						//             message:''
						//           }
						if (res.success) {
							const data = res.data;
							if (data) {
								self.list = self.list.concat(data);
							}

							if (res.has_more === false) {
								self.loadingType = 2; //2-没有更多了
							} else {
								self.loadingType = 0; //开启新一轮加载
							}

							uni.stopPullDownRefresh();

						} else {
							console.log(res.message);
						}
					}
				});
			}
		},
	}
</script>

<style>
	page,
	view {
		display: flex;
		flex-direction: column;

		/* border: #000000 solid 1px; */
	}

	.ts-media-list {
		padding: 20upx 20upx;
	}

	.ts-media-list-logo {
		padding: 0;
		width: 300upx;
		height: 230upx;
	}

	.ts-fab {
		position: fixed;
		display: block;
		z-index: 997;
		bottom: 50upx;
		right: 50upx;

		width: 100upx;
		height: 100upx;
		border-radius: 100upx;
		margin: 0 40upx;
		background-color: #e96900;
	}

	.ts-fab image {
		width: 60upx;
		height: 60upx;
		margin: 20upx;
	}

	.ts-fab text {
		position: fixed;
		bottom: 100upx;
		right: 80upx;
	}
</style>
